load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_library", "js_run_binary")
load("@rules_cc//cc:defs.bzl", "cc_library", "objc_library")
load(
    "@rules_swift//swift:swift.bzl",
    "swift_library",
)
load("//bazel:flags.bzl", "CPP_FLAGS", "MAPLIBRE_FLAGS", "WARNING_FLAGS")
load(
    "bazel/files.bzl",
    "MLN_DARWIN_OBJCPP_HEADERS",
    "MLN_DARWIN_OBJC_HEADERS",
    "MLN_DARWIN_PRIVATE_HEADERS",
    "MLN_DARWIN_PUBLIC_OBJCPP_CUSTOM_DRAWABLE_SOURCE",
    "MLN_DARWIN_PUBLIC_OBJCPP_SOURCE",
    "MLN_DARWIN_PUBLIC_OBJC_SOURCE",
    "MLN_GENERATED_DARWIN_STYLE_HEADERS",
    "MLN_GENERATED_DARWIN_STYLE_PUBLIC_HEADERS",
    "MLN_GENERATED_DARWIN_STYLE_SOURCE",
    "MLN_GENERATED_DARWIN_TEST_CODE",
)

filegroup(
    name = "darwin_private_hdrs",
    srcs = MLN_DARWIN_PRIVATE_HEADERS,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "darwin_objc_hdrs",
    srcs = MLN_DARWIN_OBJC_HEADERS,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "darwin_objcpp_hdrs",
    srcs = MLN_DARWIN_OBJCPP_HEADERS,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "darwin_objcpp_srcs",
    srcs = MLN_DARWIN_PUBLIC_OBJCPP_SOURCE,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "darwin_objcpp_custom_drawable_srcs",
    srcs = MLN_DARWIN_PUBLIC_OBJCPP_CUSTOM_DRAWABLE_SOURCE,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "darwin_objc_srcs",
    srcs = MLN_DARWIN_PUBLIC_OBJC_SOURCE,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "generated_style_srcs",
    srcs = MLN_GENERATED_DARWIN_STYLE_SOURCE,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "generated_style_hdrs",
    srcs = MLN_GENERATED_DARWIN_STYLE_HEADERS,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "generated_style_public_hdrs",
    srcs = MLN_GENERATED_DARWIN_STYLE_PUBLIC_HEADERS,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "generated_test_srcs",
    srcs = MLN_GENERATED_DARWIN_TEST_CODE,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "test_ejs_templates",
    srcs = [
        "test/MLNDocumentationGuideTests.swift",
        "test/MLNLightTest.mm.ejs",
        "test/MLNStyleLayerTests.mm.ejs",
    ],
    visibility = ["//visibility:public"],
)

filegroup(
    name = "generator_data",
    srcs = [
        "src/MLNLight.h.ejs",
        "src/MLNLight.mm.ejs",
        "src/MLNStyleLayer.h.ejs",
        "src/MLNStyleLayer.mm.ejs",
        "src/MLNStyleLayer_Private.h.ejs",
    ],
)

js_run_binary(
    name = "generated_code",
    srcs = [
        ":generator_data",
        ":test_ejs_templates",
    ],
    outs = MLN_GENERATED_DARWIN_STYLE_SOURCE +
           MLN_GENERATED_DARWIN_STYLE_HEADERS + MLN_GENERATED_DARWIN_TEST_CODE,
    tool = ":generate-style-code-script",
    visibility = ["//visibility:public"],
)

# Production-only generated code without test files
filegroup(
    name = "generated_code_production",
    srcs = MLN_GENERATED_DARWIN_STYLE_SOURCE + MLN_GENERATED_DARWIN_STYLE_HEADERS,
    visibility = ["//visibility:public"],
)

objc_library(
    name = "test_utility",
    srcs = [
        "test/MLNTestUtility.h",
    ],
    includes = ["test"],
    visibility = [
        "//platform/ios/test:__pkg__",
    ],
)

objc_library(
    name = "shared_tests_objc_srcs",
    srcs = glob(
        [
            "test/*.m",
            "test/*.h",
        ],
    ),
    copts = MAPLIBRE_FLAGS + WARNING_FLAGS["ios"],
    data = glob(
        ["test/Media.xcassets/**"],
    ),
    sdk_frameworks = [
        "MapKit",
    ],
    visibility = [
        "//platform/ios/test:__pkg__",
    ],
    deps = [
        "test_utility",
        "//platform:ios-sdk",
    ],
)

objc_library(
    name = "shared_tests_objcpp_srcs",
    srcs = glob(
        [
            "test/*.h",
            "test/*.mm",
        ],
        exclude = [
            "test/MLNOfflineStorageTests.mm",
        ],
    ),
    copts = CPP_FLAGS + MAPLIBRE_FLAGS + WARNING_FLAGS["ios"] + ["-Wno-deprecated-declarations"],
    visibility = [
        "//platform/ios/test:__pkg__",
    ],
    deps = [
        "//platform:ios-sdk",
    ],
)

swift_library(
    name = "shared_tests_swift_srcs",
    testonly = True,
    srcs = glob(["test/*.swift"]),
    data = [
        "app/PluginLayerTestStyle.json",
        "test/amsterdam.geojson",
        "test/one-liner.json",
    ],
    visibility = [
        "//platform/ios/test:__pkg__",
    ],
    deps = [
        "//platform:ios-sdk",
    ],
)

cc_library(
    name = "darwin-generated-style-artifacts",
    srcs = [
        ":darwin_objc_hdrs",
        ":darwin_objcpp_hdrs",
        ":darwin_private_hdrs",
        ":generated_code_production",
    ],
    hdrs = [
        ":generated_style_hdrs",
    ],
    visibility = ["//visibility:public"],
)

# Objc files for the iOS source
objc_library(
    name = "darwin-objc",
    srcs = [
        "core/native_apple_interface.m",
    ],
    hdrs = [
        "include/mbgl/interface/native_apple_interface.h",
        "include/mbgl/util/image+MLNAdditions.hpp",
    ],
    copts = WARNING_FLAGS["ios"] + MAPLIBRE_FLAGS + [
        "-fmodules",
    ],
    defines = [
        "GLES_SILENCE_DEPRECATION=1",
        "GL_SILENCE_DEPRECATION=1",
    ],
    includes = [
        "include",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "//:mbgl-core",
        "//platform/default:mbgl-default",
    ],
)

# Objcpp files for the iOS source
objc_library(
    name = "darwin-objcpp",
    srcs = [
        "core/collator.mm",
        "core/http_file_source.mm",
        "core/image.mm",
        "core/local_glyph_rasterizer.mm",
        "core/logging_nslog.mm",
        "core/nsthread.mm",
        "core/number_format.mm",
        "core/string_nsstring.mm",
        "src/CFHandle.hpp",
    ] + select({
        "//:metal_renderer": [],
        "//conditions:default": ["src/headless_backend_eagl.mm"],
    }),
    hdrs = [
        "include/mbgl/interface/native_apple_interface.h",
        "include/mbgl/util/image+MLNAdditions.hpp",
    ],
    copts = CPP_FLAGS + MAPLIBRE_FLAGS + [
        "-fcxx-modules",
        "-fmodules",
        "-Wno-vla-extension",
        "-Wno-gnu-conditional-omitted-operand",
    ],
    includes = [
        "include",
        "src",
    ],
    sdk_frameworks = [
        "CoreText",
        "ImageIO",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "darwin-objc",
        "//:mbgl-core",
        "//platform/default:mbgl-default",
    ],
)

# Loop files for iOS.
objc_library(
    name = "darwin-loop",
    srcs = [
        "core/async_task.cpp",
        "core/run_loop.cpp",
        "core/timer.cpp",
    ] + select({
        "//:metal_renderer": [],
        "//conditions:default": ["src/gl_functions.cpp"],
    }),
    copts = CPP_FLAGS + MAPLIBRE_FLAGS,
    defines = [
        "GLES_SILENCE_DEPRECATION=1",
        "GL_SILENCE_DEPRECATION=1",
    ],
    sdk_frameworks = [
        "SystemConfiguration",
    ] + select({
        "//:metal_renderer": ["Metal"],
        "//conditions:default": ["OpenGLES"],
    }),
    visibility = ["//visibility:public"],
    deps = [
        "//:mbgl-core",
    ],
)

exports_files(
    [
        "app/PluginLayerTestStyle.json",
        "app/PluginLayerExample.h",
        "app/PluginLayerExample.mm",
        "app/PluginLayerExampleMetalRendering.h",
        "app/PluginLayerExampleMetalRendering.mm",
        "test/amsterdam.geojson",
        "test/MLNSDKTestHelpers.swift",
        "app/CustomStyleLayerExample.h",
        "app/CustomStyleLayerExample.m",
        "app/ExampleCustomDrawableStyleLayer.h",
        "app/ExampleCustomDrawableStyleLayer.mm",
        "include/mbgl/util/image+MLNAdditions.hpp",
    ] + MLN_DARWIN_PUBLIC_OBJC_SOURCE +
    MLN_DARWIN_PUBLIC_OBJCPP_SOURCE +
    MLN_DARWIN_PUBLIC_OBJCPP_CUSTOM_DRAWABLE_SOURCE +
    MLN_DARWIN_PRIVATE_HEADERS +
    MLN_DARWIN_OBJC_HEADERS +
    MLN_DARWIN_OBJCPP_HEADERS,
)

js_binary(
    name = "generate-style-code-script",
    data = [
        "scripts/style-spec-cocoa-conventions-v8.json",
        "scripts/style-spec-overrides-v8.json",
        "//:node_modules/csscolorparser",
        "//:node_modules/lodash",
        "//:style-code",
        "//:style-spec",
    ],
    entry_point = "scripts/generate-style-code.mjs",
)

js_library(
    name = "symbols-to-check",
    srcs = [
        ":darwin_objc_hdrs",
        ":darwin_objc_srcs",
        ":darwin_objcpp_hdrs",
        ":darwin_objcpp_srcs",
        ":darwin_private_hdrs",
        ":generated_code_production",
        ":generated_style_hdrs",
        ":generated_style_srcs",
    ] + select({
        "//conditions:default": [
            ":darwin_objcpp_custom_drawable_srcs",
        ],
    }),
    visibility = ["//platform:__pkg__"],
)
